<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class ArticleTwo extends AdminBase
{
    public function title(){
        $model=Db::name('title')
            ->where(['type'=>4,'language_type'=>$this->language_type])
            ->find();
        return view('title',['model'=>$model]);
    }
    public function title_add(){
        $data=input('post.');
        $res=Db::name('title')
            ->where(['type'=>4,'language_type'=>$this->language_type])
            ->update([
                'title'=>$data['title'],
                'sub_title'=>$data['sub_title'],
            ]);
        if (!$res){
            $data['type']=4;
            $data['language_type']=$this->language_type;
            $res=Db::name('title')
                ->insert($data);
            if (!$res){
                return show(0,'保存失败');
            }
        }
        return show(1,'保存成功');
    }
    public function index()
    {
        $model=Db::name('article')
            ->where(['language_type'=>$this->language_type,'type'=>2])
            ->order(['status'=>'ASC','sort'=>'DESC'])
            ->select()
            ->toArray();
        return View::fetch("article_list",['model'=>$model]);
    }
    public function status(){
        $data=input('post.');
        $model=Db::name('article')
            ->where('id',$data['id'])
            ->update(['status'=>$data['status']]);
        if (!$model){
            return show(0);
        }
        return show(1);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['content']=$data['editorValue'];
            unset($data['editorValue']);
            if ($data['id']) {
                $data['update_time']=time();
                $res = Db::name('article')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $data['create_time']=time();
                $data['update_time']=time();
                $data['type']=2;
                $data['language_type']=$this->language_type;
                $res = Db::name('article')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $model = Db::name('article')
                ->where('id', $id)
                ->find();
            return View::fetch('article_add', ['model' => $model]);
        }
    }
    public function del(){
        $res=Db::name('article')
            ->delete(input('id'));
        if ($res){
            return show(1,'删除成功');
        }
        return show(0,'删除失败');
    }
}